/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mybean;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MrMAD
 */
public class Customer {

    String name;
    String password;
    String email;
    String phone;
    String address;
    String company;
    String DoB;

    public Customer() {
    }

    public Customer(String name) {
        this.name = name;
    }

    public Customer(String email, String password) {
        this.email = email;
        this.password = password;
    }

    public Customer(String name, String password, String email, String phone, String address, String company, String DoB) {
        this.name = name;
        this.password = password;
        this.email = email;
        this.phone = phone;
        this.address = address;
        this.company = company;
        this.DoB = DoB;
    }

    public void setCustomer(String name, String email, String phone, String address, String company, String DoB) {
        this.name = name;
        this.email = email;
        this.phone = phone;
        this.address = address;
        this.company = company;
        this.DoB = DoB;
    }        

    public Customer(String name, String password, String email, String phone, String address, String company) {
        this.name = name;
        this.password = password;
        this.email = email;
        this.phone = phone;
        this.address = address;
        this.company = company;
    }

    public Customer(String name, String email, String phone, String address, String company) {
        this.name = name;
        this.email = email;
        this.phone = phone;
        this.address = address;
        this.company = company;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getDoB() {
        return DoB;
    }

    public void setDoB(String DoB) {
        this.DoB = DoB;
    }

    public boolean authenticate() {
        if (this.email == "" || this.password == "") {
            return false;
        }
        try {
            String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=BookStore;user=sa;password=sa";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(connectionUrl);

            String SQL = "SELECT * FROM Customer WHERE email='" + this.email + "'";
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);

            String validPass = "";
            while (rs.next()) {
                validPass = rs.getString("password");
            }
            boolean isValid = this.password.equals(validPass);

            return isValid;
        } catch (ClassNotFoundException ex) {
            return false;
        } catch (SQLException ex) {
            return false;
        }
    }

    @Override
    public String toString() {
        return "Customer{" + "name=" + name + ", password=" + password + ", email=" + email + ", phone=" + phone + ", address=" + address + ", company=" + company + ", DoB=" + DoB + '}';
    }

    public Customer getInfoByName() {
        if ("".equals(this.name) || this.name == null) {
            return null;
        }
        try {
            String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=BookStore;user=sa;password=sa";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(connectionUrl);

            String SQL = "SELECT * FROM Customer WHERE name='" + this.name + "'";
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);

            while (rs.next()) {
                this.email = rs.getString("email");
                this.phone = rs.getString("phone");
                this.address = rs.getString("address");
                this.company = rs.getString("company");
                this.DoB = rs.getString("DoB");
            }
            return this;
        } catch (ClassNotFoundException ex) {
            return null;
        } catch (SQLException ex) {
            return null;
        }
    }

    public void getInfoByEmail() {
        if ("".equals(this.email) || this.email == null) {
            return;
        }
        try {
            String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=BookStore;user=sa;password=sa";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(connectionUrl);

            String SQL = "SELECT * FROM Customer WHERE email='" + this.email + "'";
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);

            while (rs.next()) {
                this.name = rs.getString("name");
                this.phone = rs.getString("phone");
                this.address = rs.getString("address");
                this.company = rs.getString("company");
                this.DoB = rs.getString("DoB");
            }
            return;
        } catch (ClassNotFoundException ex) {
        } catch (SQLException ex) {
        }
    }
    
    public void getInfoByEmail(String email) {
        String result = "";
        if ("".equals(email) || email == null) {
            result += "Empty email";
            return;
        }
        try {
            String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=BookStore;user=sa;password=sa";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(connectionUrl);

            String SQL = "SELECT * FROM Customer WHERE email='" + email + "'";
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);

            while (rs.next()) {
                this.email = email;
                this.name = rs.getString("name");
                this.phone = rs.getString("phone");
                this.address = rs.getString("address");
                this.company = rs.getString("company");
                this.DoB = rs.getString("DoB");
            }
            result += "done";
        } catch (ClassNotFoundException ex) {
            result += ex.getMessage();
        } catch (SQLException ex) {
            result += ex.getMessage();
        }        
    }

    public boolean register() {
        try {
            String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=BookStore;user=sa;password=sa";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(connectionUrl);

            String insertQuery = "insert into Customer values (?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement insertStatement = con.prepareStatement(insertQuery);
            insertStatement.setString(1, this.name);
            insertStatement.setString(2, this.password);
            insertStatement.setString(3, this.email);
            insertStatement.setString(4, this.address);
            insertStatement.setString(5, this.phone);
            insertStatement.setString(6, this.company);
            insertStatement.setString(7, this.DoB);
            insertStatement.execute();
            
            return true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean validEmail() {
        try {
            String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=BookStore;user=sa;password=sa";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(connectionUrl);
            
            String SQL = "SELECT * FROM Customer WHERE email='" + this.email + "'";
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);
            
            while(rs.next()){
                return false;
            }
            return true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
}
